package com.space.controller;

import com.space.entity.*;
import com.space.exception.MyException;
import com.space.message.MyMessage;
import com.space.service.AdminService;
import com.space.service.CityService;
import com.space.service.FlightService;
import com.space.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : 港某
 * @date : 2020/2/12 11:11
 */
@Controller
@RequestMapping("space/admin")
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;
    @Autowired
    CityService cityService;
    @Autowired
    FlightService flightService;

    @RequestMapping("adminLogin")
    public String adminLogin(Admin admin, HttpServletRequest request) {
        Admin newAdmin = adminService.adminLogin(admin);
        if (newAdmin != null) {
            request.getSession().setAttribute("adminLogin", admin);
            return "redirect:index.jsp";
        }
        return "redirect:adminLogin.jsp";
    }

    @RequestMapping("exitAdminSystem")
    public String exitAdminSystem(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:adminLogin.jsp";
    }

    @RequestMapping("checkAdminAccount")
    @ResponseBody
    public AjaxResult checkAdminAccount(String adminAccount) {
        Admin admin = adminService.selectAdminByAccount(adminAccount);
        if (admin == null) {
            return new AjaxResult(false, MyMessage.ADMIN_ACCOUNT_ERROR);
        }
        return new AjaxResult(true, "");
    }

    @RequestMapping("checkAdminPwd")
    @ResponseBody
    public AjaxResult checkAdminPwd(String adminAccount, String adminPwd) {
        boolean bool = adminService.checkAdminPwd(adminAccount, adminPwd);
        if (!bool) {
            return new AjaxResult(false, MyMessage.ADMIN_PASSWORD_ERROR);
        }
        return new AjaxResult(true, "");
    }

    @RequestMapping("getAllUser")
    public String getAllUser(ModelMap mm) {
        List<User> userList = userService.selectAllUser();
        mm.addAttribute("userList", userList);
        return "/admin/userManager";
    }

    @RequestMapping("updateUserStatus")
    public String updateUserStatus(int[] userId, int userStatusId) {
        userService.updateUserStatusById(userId, userStatusId);
        return "redirect:getAllUser.do";
    }

    @RequestMapping("toUpdateUserInfoJsp")
    public String toUpdateUserInfoJsp(int userId, HttpServletRequest request) {
        User user = userService.selectUserById(userId);
        request.getSession().setAttribute("user", user);
        return "/admin/updateUser";
    }

    @RequestMapping("updateUser")
    public String updateUser(User user, String userPwd2, HttpServletRequest request, ModelMap mm) {
        User oldUser = (User) request.getSession().getAttribute("user");
        try {
            userService.updateUserInfo(oldUser, user, userPwd2);
        } catch (MyException e) {
            mm.addAttribute("msg", e.getMessage());
        }
        return "forward:getAllUser.do";
    }

    @RequestMapping("addFlight")
    public String addFlight(String flightStartAreaId, String flightEndAreaId, String flightStartTimeAdd, String flightPrice, ModelMap modelMap) {
        try {
            flightService.insertFlight(flightStartAreaId, flightEndAreaId, flightStartTimeAdd, flightPrice);
        } catch (MyException e) {
            modelMap.addAttribute("msg", e.getMessage());
        }
        return "forward:selectFlightByPage.do";
    }

    @RequestMapping("deleteFlight")
    public String deleteFlight(int[] flightIdArr) {
        flightService.deleteFlightById(flightIdArr);
        return "forward:selectFlightByPage.do";
    }

    @RequestMapping("selectFlightByPage")
    public String selectFlightByPage(String nowPage, String flightStartTime, String flightStartArea,
                                     String flightEndArea, ModelMap mm) {
        Rflight rflight = flightService.selectSomeFlightByPage(nowPage, flightStartTime, flightStartArea, flightEndArea);
        List<City> cityList = cityService.selectAllCity();
        mm.addAttribute("flightList", rflight.getFlightList());
        mm.addAttribute("cityList", cityList);
        mm.addAttribute("flightStartTime", flightStartTime);
        mm.addAttribute("flightStartArea", flightStartArea);
        mm.addAttribute("flightEndArea", flightEndArea);
        mm.addAttribute("nowPage", nowPage);
        mm.addAttribute("pageCount", rflight.getPageCount());
        return "/admin/flightManager";
    }

    @RequestMapping("toFlightCheckJsp")
    public String toFlightCheckJsp(ModelMap mm) {
        List<Flight> flightList = flightService.selectAllFlight();
        List<City> cityList = cityService.selectAllCity();
        FlightStatusCount flightStatusCount = flightService.selectSomeFlightCount();
        mm.addAttribute("flightList", flightList);
        mm.addAttribute("cityList", cityList);
        mm.addAttribute("flightStatusCount", flightStatusCount);
        return "/admin/flightCheck";
    }

    @RequestMapping("toUpdateFlightJsp")
    public String toUpdateFlightJsp(int flightId, ModelMap mm) {
        Flight flight = flightService.selectFlightById(flightId);
        if (flight.getFlightStatus().getStatusTypeId() != 1) {
            mm.addAttribute("msg", MyMessage.FLIGHT_UPDATE_ERROR);
            return "forward:selectFlightByPage.do";
        }
        mm.addAttribute("flight", flight);
        return "/admin/updateFlight";
    }

    @RequestMapping("updateFlight.do")
    public String updateFlight(int flightId, String flightStartTime, String flightPrice) {
        flightService.updateFlightById(flightId, flightStartTime, flightPrice);
        return "redirect:selectFlightByPage.do";
    }


}
